home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 2000 January
/
Macworld (2000-01).dmg
/
Shareware World
/
Info
/
For Developers
/
OSA Script.sea
/
OSA Script
/
ExecuteScript.p
< prev
next >
Wrap
Text File
|
1999-10-21
|
4KB
|
97 lines
Program ExecuteOSAScriptWithParams;
{©23/7/99 — Jan Skarbek. http://www.ozemail.com.au/~pbird. pbird@ozemail.com.au}
{You are welcome to distribute this AppleEvent handling sample freely, but must keep the package intact.}
{Comments, questions, and suggestions invited.}
{An example of how to call a user-defined handler within a script object ('scpt').}
{The script, in this case, was copied out of the accompanying compiled script sample, with ResEdit,}
{and placed in the Execute Script.rsrc file.}
{This sample only shows how to call a handler with a simple direct parameter: a list. It is possible to call a handler}
{defined by name, rather than by event codes, but that's not covered in this example.}
{Note that you should set your 'SIZE' resource to indicate that you accept high-level events, tho it's not}
{strictly necessary if the script exists only as a part of your application and doesn't call any outside processes.}
{Failure to do so will prevent the AEM from replying to you. You should also allocate at least 200KB to the app.}
{ Added WRITELN output to display actions and results of program, and added exit when error detected. }
{ Bill Catambay, bill.m.catambay@lmco.com, 10/21/99 }
Uses
Processes, AppleEvents, OSA, ExecuteEventInScript;
Const
scriptResID = 128; {the ID of the 'scpt' resource, as copied from the script sample}
Var
err, ie: OSErr;
a, b, c, x, y, z: longint;
desc1, desc2: AEDescList;
theAEKey: AEKeyWord;
actualSize: Size;
typeCode: DescType;
Procedure ErrorExit(err: OSErr);
begin
writeln('ERROR EXCEPTION: Error #',err:1);
ExitToShell;
end; { of ErrorExit }
Begin
a := 1;
b := 2;
c := 3;
x := 0;
y := 0;
z := 0;
writeln('ExecuteScript Sample Pascal Program');
writeln('ACTION: Pass in parameters a, b and c to target script "The Script to Execute"');
writeln('EXPECTED RESULTS: Script increments values and stores results in x, y and z');
writeln;
err := noErr;
desc1.dataHandle := nil; {Always null your AEDescs!}
desc2.dataHandle := nil;
{create an empty parameter list for the script handler}
writeln('Creating descriptor...');
err := AECreateList(nil, 0, false, desc1);
if err <> noErr then
ErrorExit(err);
{add the parameters to the list}
writeln('Adding arguments to descriptor (a=',a:1,', b=',b:1,', c=',c:1,')...');
err := AEPutPtr(desc1, 0, typeLongInteger, @a, SizeOf(a));
if err <> noErr then
ErrorExit(err);
err := AEPutPtr(desc1, 0, typeLongInteger, @b, SizeOf(b));
if err <> noErr then
ErrorExit(err);
err := AEPutPtr(desc1, 0, typeLongInteger, @c, SizeOf(c));
if err <> noErr then
ErrorExit(err);
{execute the handler within the script, whose Class is 'abcd' and whose ID is '1234'}
writeln('Executing script...');
err := ExecuteEventInCompiledScriptResource(scriptResID, desc1, desc2, 'abcd', '1234');
if err <> noErr then
ErrorExit(err);
{extract the result from the executed script. desc2 holds the returned values}
writeln('Extracting results and storing in x, y and z...');
err := AEGetNthPtr(desc2, 1, typeLongInteger, theAEKey, typeCode, @x, SizeOf(x), actualSize);
if err <> noErr then
ErrorExit(err);
err := AEGetNthPtr(desc2, 2, typeLongInteger, theAEKey, typeCode, @y, SizeOf(x), actualSize);
if err <> noErr then
ErrorExit(err);
err := AEGetNthPtr(desc2, 3, typeLongInteger, theAEKey, typeCode, @z, SizeOf(x), actualSize);
if err <> noErr then
ErrorExit(err);
{clean up}
ie := AEDisposeDesc(desc1);
ie := AEDisposeDesc(desc2);
writeln;
writeln('RESULTS: x=',x:1,', y=',y:1,', z=',z:1);
end.